package org.pcorp.battlespace.construction.ui.sub.structure;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.metal.MetalComboBoxButton;
import javax.swing.text.NumberFormatter;

import org.pcorp.battlespace.construction.modele.ConstructionUtils;
import org.pcorp.battlespace.construction.modele.VaisseauCstr;
import org.pcorp.battlespace.construction.ui.Principal;
import org.pcorp.battlespace.construction.ui.listener.MasseListener;
import org.pcorp.battlespace.construction.ui.tools.ConstanteUI;
import org.pcorp.battlespace.construction.ui.tools.GeneralRuleUi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GeneralUi extends GeneralRuleUi {
	private static final long serialVersionUID = -3947692647867017865L;
	private JTextField classeInp;
	private JComboBox typeInp;
	private JFormattedTextField masseInp;
	private static final Logger _log = LoggerFactory.getLogger(GeneralUi.class);
	
	public GeneralUi(Principal p) {
		super(p);
		setLayout(new GridBagLayout());
		build();
	}
	
	private void build() {
	    TitledBorder border=BorderFactory.createTitledBorder(" Description ");
	    border.setTitleColor(ConstanteUI.LABEL_COLOR);
	    setBorder(border);
		
		JLabel classeLbl = getLabel("Classe");
		JLabel typeLbl   = getLabel("Type");
		JLabel masseLbl  = getLabel("Masse");
		
		classeInp = getJTextField();
		typeInp   = new JComboBox(ConstructionUtils.TYPE);
		
		masseInp  = new JFormattedTextField(new NumberFormatter());
		initJText(masseInp);
		
		initListener();
		
		GridBagConstraints gbc = new GridBagConstraints();

	    gbc.gridx = gbc.gridy = 0;
	    gbc.gridwidth = gbc.gridheight = 1;
	    gbc.weightx = gbc.weighty = 0;
	    
	    gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
	    gbc.fill = GridBagConstraints.NONE;
	    gbc.gridx = 0;
	    gbc.insets = new Insets(1, 10, 1, 10);
	    
	    gbc.gridy = 0;
	    add(classeLbl, gbc);
	    gbc.gridy = 1;
	    add(typeLbl, gbc);
	    gbc.gridy = 2;
	    add(masseLbl, gbc);
	    
	    gbc.weightx = 1;
	    gbc.fill = GridBagConstraints.NONE;
	    gbc.anchor = GridBagConstraints.BASELINE_LEADING;
	    gbc.insets = new Insets(1, 0, 1, 10);
	    gbc.gridx = 1;
	    gbc.gridy = 0;
	    add(classeInp, gbc);
	    gbc.gridy = 1;
	    add(typeInp, gbc);
	    gbc.gridy = 2;
	    add(masseInp, gbc);
	    
	}

	private void initListener() {
		MasseListener masseLt = new MasseListener(p);
		masseInp.getDocument().addDocumentListener(masseLt);
		masseInp.addFocusListener(masseLt);
		
		typeInp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				_log.debug("action performed : {}", e.getActionCommand());
				if (e.getModifiers()==0) {
					return;
				}
				p.updateModel(null);
				p.updateSabord();
				p.updateDisplay(null);
			}
		});
	}

	@Override
	public void updateDisplay(VaisseauCstr vaiss) {
		classeInp.setText(vaiss.getNomClasse());
		masseInp.setText(Long.toString(vaiss.getMasse()));
		if (typeInp==null || typeInp.getSelectedItem()==null)
			return;
		if (!typeInp.getSelectedItem().equals(vaiss.getType()))
			typeInp.setSelectedItem(vaiss.getType());
	}

	@Override
	public void updateModel(VaisseauCstr vaiss) {
		vaiss.setNomClasse(classeInp.getText());
		vaiss.setMasse(getLong(masseInp));
		if (typeInp.getSelectedItem() != null)
			vaiss.setType(typeInp.getSelectedItem().toString());
	}
}
